﻿@model IEnumerable<Superstore.Store.Models.DeliveryAddrInfo>

<section id="GetDeliveryAddressList" class="addressList">
    <ul>
        @foreach (var item in Model)
        {
            <li id="li_item_@item.id" onclick="selectItem(@item.id)">
                @if (item.id == ViewBag.addrId)
                {
                    <div class="item active">
                        <div class="lay_center">
                            <span class="item_name" id='name_@item.id'>@Html.DisplayFor(modelItem => item.username)</span>
                            <span class="item_number" id="number_@item.id">@Html.DisplayFor(modelItem => item.userphone)</span><br />
                            <span class="item_address" id="address_@item.id">@Html.DisplayFor(modelItem => item.deliveryaddr)</span>
                            <span style="display:none;" id="sex_@item.id">@Html.DisplayTextFor(modelItem => item.sex)</span>
                        </div>
                        <a class="btn-edit" href="@Url.Content("~/")' + 'DeliveryAddr/Edit?id=' + @item.id + '&name=' + @item.username + '&sex=' + @item.sex + '&phone=' + @item.userphone + '&address=' + @item.deliveryaddr"><img src="/images/DeliveryAddrEdit.png"></a>
                        <a class="btn-edit" onclick="editDeliveryAddr(@item.id)"><img src="/images/DeliveryAddrEdit.png"></a>
                        <a class="btn-delete" id="@item.id" onclick="delDeliveryAddr(@item.id)" style="display: none;"><img src="/images/DeliveryAddrDel.png"></a>
                    </div>
                }
                else
                {
                    <div class="item">
                        <div class="lay_center">
                            <span class="item_name" id='name_@item.id'>@Html.DisplayFor(modelItem => item.username)</span>
                            <span class="item_number" id="number_@item.id">@Html.DisplayFor(modelItem => item.userphone)</span><br />
                            <span class="item_address" id="address_@item.id">@Html.DisplayFor(modelItem => item.deliveryaddr)</span>
                            <span style="display:none;" id="sex_@item.id">@Html.DisplayTextFor(modelItem => item.sex)</span>
                        </div>
                        <a class="btn-edit" href="@Url.Content("~/")' + 'DeliveryAddr/Edit?id=' + @item.id + '&name=' + @item.username + '&sex=' + @item.sex + '&phone=' + @item.userphone + '&address=' + @item.deliveryaddr"><img src="/images/DeliveryAddrEdit.png"></a>
                        <a class="btn-edit" onclick="editDeliveryAddr(@item.id)"><img src="/images/DeliveryAddrEdit.png"></a>
                        <a class="btn-delete" id="@item.id" onclick="delDeliveryAddr(@item.id)" style="display: none;"><img src="/images/DeliveryAddrDel.png"></a>
                    </div>
                }
            </li>
        }
    </ul>
</section>

<script>
    function selectItem(id) {
        $("#GetDeliveryAddressList>ul>li>div").removeClass("active");
        $("#li_item_" + id + ">div").addClass("active");

        var index = $("#GetDeliveryAddressList>ul>li").index($("#li_item_" + id));
        DeliveryAddrMgr.setDeliveryAddrIndex(index);

        var location ="@ViewBag.redirectUrl";
        console.log(location);
        if (location != "") {
            window.location.href = location;
        }
    }

    function editDeliveryAddr(id) {
        var index = $("#GetDeliveryAddressList>ul>li").index($("#li_item_" + id));
        DeliveryAddrMgr.setEditDeliveryIndex(index);
        window.location.href = '@Url.Content("~/")' + 'DeliveryAddr/Edit';
    }

    function deleteDeliveryAddrCallback(deliveryAddrId) {
        $("#li_item_" + deliveryAddrId).remove();
    }

    function delDeliveryAddr(deliveryAddrId) {
        DeliveryAddrMgr.deleteDeliveryAddr(deliveryAddrId, deleteDeliveryAddrCallback);
    }
</script>
